//
//  HMStatusCell.m
//  SinaWeiBo
//
//  Created by WGP on 16/4/15.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "HMStatusCell.h"
#import "HMStatusOriginalView.h"
#import "HMStatusBottomView.h"
#import "HMStatusRetweetView.h"
#import "HMStatuses.h"


@interface HMStatusCell ()

@property(nonatomic,strong)HMStatusOriginalView *originalView;
@property(nonatomic,strong)HMStatusRetweetView *retweetView;
@property(nonatomic,strong)HMStatusBottomView *bottomView;
@property(nonatomic,strong)MASConstraint *bottomTopCons;

@end

@implementation HMStatusCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return  self;
}


-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setupUI];
    }
    return  self;
}



-(void)setupUI
{
    self.backgroundColor = HMGlobolColor;
    
//    1.添加控件
    HMStatusOriginalView *originalView = [[HMStatusOriginalView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    self.originalView = originalView;
    [self.contentView addSubview:originalView];
    
    self.bottomView = [[HMStatusBottomView alloc] init];
    [self.contentView addSubview:self.bottomView];
    
    self.retweetView = [[HMStatusRetweetView alloc] init];
    [self.contentView addSubview:self.retweetView];

    
//    2.添加约束
    [originalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.top.equalTo(self.contentView).offset(HMStatusCellMargin);
    }];
    
    [self.retweetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.top.equalTo(self.originalView.mas_bottom);
    }];
    
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.height.mas_equalTo(37);
        
        make.top.equalTo(self.retweetView.mas_bottom);
//        self.bottomTopCons = make.top;
    }];
    
    
//    就当给一个控件里的子控件设置约束,contenview也是cell的一个子控件
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.bottom.equalTo(self.bottomView);
        
    }];
    
    
//    图层异步绘制
    self.layer.drawsAsynchronously = YES;
    
//    栅格化:将view截图成一个bitmap.只要内容不变,就直接回使用bitmap,会产生离屏渲染
    self.layer.shouldRasterize = true;
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
}

-(void)setStatus:(HMStatuses *)status
{
    _status = status;
    self.originalView.status =status;
    
//    [self.bottomTopCons uninstall];
    
    if(status.retweeted_status != nil)
    {
        self.retweetView.hidden = NO;
        self.retweetView.status = status.retweeted_status;
        
        [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
           make.top.equalTo(self.retweetView.mas_bottom);
//            self.bottomTopCons = make.top;
            
            make.left.right.equalTo(self.contentView);
            make.height.mas_equalTo(37);
        }];
        
    }else
    {
        self.retweetView.hidden = YES;
        
        [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.originalView.mas_bottom);
//            self.bottomTopCons = make.top;
            
            make.left.right.equalTo(self.contentView);
            make.height.mas_equalTo(37);
            
        }];
    }
}

@end












